﻿using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;

public class Item
{
    public int id;
    public int num;
}

public class PlayerInfo
{
    public string name;
    public int atk;
    public int def;
    public float moveSpeed;
    public float roundSpeed;
    public Item weapon;
    public List<int> listInt;
    public List<Item> itemList;
    public Dictionary<int, Item> itemDic;

    public void LoadData(string fileName)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(Application.streamingAssetsPath + "/PlayerInfo.xml");

        //加载根节点
        XmlNode playerInfo = xml.SelectSingleNode("PlayerInfo");
        //通过根节点 加载具体的信息
        this.name = playerInfo.SelectSingleNode("name").InnerText;
        this.atk = int.Parse(playerInfo.SelectSingleNode("atk").InnerText);
        this.def = int.Parse(playerInfo.SelectSingleNode("def").InnerText);
        this.moveSpeed = float.Parse(playerInfo.SelectSingleNode("moveSpeed").InnerText);
        this.roundSpeed = float.Parse(playerInfo.SelectSingleNode("roundSpeed").InnerText);
        //weapon
        weapon = new Item();
        XmlNode nodeWeapon = playerInfo.SelectSingleNode("weapon");
        weapon.id = int.Parse(nodeWeapon.SelectSingleNode("id").InnerText);
        weapon.num = int.Parse(nodeWeapon.SelectSingleNode("num").InnerText);
        //listInt
        XmlNode nodeListInt = playerInfo.SelectSingleNode("listInt");
        XmlNodeList intList = nodeListInt.SelectNodes("int");
        this.listInt = new List<int>();
        foreach (XmlNode item in intList)
        {
            listInt.Add(int.Parse(item.InnerText));
        }
        //itemList
        XmlNode nodeItem = playerInfo.SelectSingleNode("itemList");
        XmlNodeList items = nodeItem.SelectNodes("Item");
        this.itemList = new List<Item>();
        foreach (XmlNode item in items)
        {
            itemList.Add(new Item() { id = int.Parse(item.Attributes["id"].Value), num = int.Parse(item.Attributes["num"].Value) });
        }
        //itemDic
        this.itemDic = new Dictionary<int, Item>();
        XmlNode ItemDic = playerInfo.SelectSingleNode("itemDic");
        XmlNodeList dicInts = ItemDic.SelectNodes("int");
        XmlNodeList dicItems = ItemDic.SelectNodes("Item");
        for (int i=0; i<dicInts.Count; i++)
        {
            int key = int.Parse(dicInts[i].InnerText);
            this.itemDic.Add(key, new Item() { id = int.Parse(dicItems[i].Attributes["id"].Value), num = int.Parse(dicItems[i].Attributes["num"].Value) });
        }
    }

    public void SaveData(string fileName)
    {
        string path = string.Format("{0}/{1}", Application.persistentDataPath, fileName);
        XmlDocument xml = new XmlDocument();
        XmlDeclaration xmlDec = xml.CreateXmlDeclaration("1.0", "UTF-8", "");
        xml.AppendChild(xmlDec);

        XmlElement root = xml.CreateElement("PlayerInfo");
        xml.AppendChild(root);

        XmlElement name = xml.CreateElement("name");
        name.InnerText = this.name;
        root.AppendChild(name);

        XmlElement atk = xml.CreateElement("atk");
        atk.InnerText = this.atk.ToString();
        root.AppendChild(atk);

        XmlElement def = xml.CreateElement("def");
        def.InnerText = this.def.ToString();
        root.AppendChild(def);

        XmlElement moveSpeed = xml.CreateElement("moveSpeed");
        moveSpeed.InnerText = this.moveSpeed.ToString();
        root.AppendChild(moveSpeed);

        XmlElement roundSpeed = xml.CreateElement("roundSpeed");
        roundSpeed.InnerText = this.roundSpeed.ToString();
        root.AppendChild(roundSpeed);

        XmlElement weapon = xml.CreateElement("weapon");
        XmlElement weapon_id = xml.CreateElement("id");
        weapon_id.InnerText = this.weapon.id.ToString();
        weapon.AppendChild(weapon_id);
        XmlElement weapon_num = xml.CreateElement("num");
        weapon_num.InnerText = this.weapon.num.ToString();
        weapon.AppendChild(weapon_num);
        root.AppendChild(weapon);

        XmlElement listInt = xml.CreateElement("listInt");
        for (int i=0; i<this.listInt.Count; i++)
        {
            XmlElement item = xml.CreateElement("int");
            item.InnerText = this.listInt[i].ToString();
            listInt.AppendChild(item);
        }
        root.AppendChild(listInt);

        XmlElement itemList = xml.CreateElement("itemList");
        for (int i = 0; i < this.itemList.Count; i++)
        {
            XmlElement item = xml.CreateElement("Item");
            item.SetAttribute("id", this.itemList[i].id.ToString());
            item.SetAttribute("num", this.itemList[i].num.ToString());
            itemList.AppendChild(item);
        }
        root.AppendChild(itemList);

        XmlElement itemDic = xml.CreateElement("itemDic");
        foreach (KeyValuePair<int, Item> item in this.itemDic)
        {
            XmlElement key = xml.CreateElement("int");
            key.InnerText = item.Key.ToString();
            itemDic.AppendChild(key);

            XmlElement val = xml.CreateElement("Item");
            val.SetAttribute("id", item.Value.id.ToString());
            val.SetAttribute("num", item.Value.num.ToString());
            itemDic.AppendChild(val);
        }
        root.AppendChild(itemDic);
        xml.Save(path);
    }
}
